package cn.laixueit.common;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;
import java.util.Random;
import java.util.UUID;

public class Utils {
    public static String uuid(){
        return UUID.randomUUID().toString().replace("-","").substring(0,16);
    }

    public static  Long hashId(){
        Random random = new Random();
        Long hashid = Long.valueOf(random.nextInt(999999999));
        if(hashid<1000000000){
            hashid+=1000000000;
        }
        return hashid;
    }

    public static  Integer rancode(){
        Random random = new Random();
        Integer code = Integer.valueOf(random.nextInt(999999));
        if(code<100000){
            code+=100000;
        }
        return code;
    }


    public static Integer sendMail(String email){
        Integer code = rancode();
        try {
            Properties properties = new Properties();
            //设置服务器名称
            properties.setProperty("mail.host", "smtp.163.com");
            //设置邮件传输协议
            properties.setProperty("mail.transport.protocol", "smtp");
            Session session = Session.getInstance(properties);
            MimeMessage mimeMessage = new MimeMessage(session);

            mimeMessage.setSubject("易购书城账号激活");


            MimeMultipart multipart = new MimeMultipart();

            MimeBodyPart bodyPart = new MimeBodyPart();

            bodyPart.setContent("激活码<font color=red size=6>"+code+"</font>","text/html;charset=utf-8");

            multipart.addBodyPart(bodyPart);

            mimeMessage.setContent(multipart);


            mimeMessage.setFrom(new InternetAddress("15618816293@163.com"));

            mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));


            Transport transport = session.getTransport("smtp");

            //10.设置发件人的信息
            transport.connect("smtp.163.com", "15618816293@163.com", "WOYPWTAXHOHEFMTZ");

            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return code;
    }

    public static void main(String[] args) throws Exception {

        System.out.println(rancode());
    }
}
